module.exports = class SystemFile {
    static ins=null;

    static getIns(mainWindow){
        if(!SystemFile.ins){
            SystemFile.ins=new SystemFile(mainWindow);
        }
        return SystemFile.ins;
    }
    mainWindow = null;

    constructor(mainWindow) {
        this.mainWindow = mainWindow;
    }
    /**
     * 读取文件内容
     * @param filePath {string} 文件路径
     * @returns {Promise<unknown>}
     */
    async readFile(filePath) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.readFile(filePath, 'utf8', (err, data) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(data);
                }
            });
        });
    }

    /**
     * 写入文件内容
     * @param filePath {string} 文件路径
     * @param data {string} 文件内容
     * @returns {Promise<void>}
     */
    async writeFile(filePath, data) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.writeFile(filePath, data, 'utf8', (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }

    /**
     * 删除文件
     * @param filePath {string} 文件路径
     * @returns {Promise<void>}
     */
    async deleteFile(filePath) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.unlink(filePath, (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }

    /**
     * 判断文件是否存在
     * @param filePath {string} 文件路径
     * @returns {Promise<boolean>}
     */
    async exists(filePath) {
        const path = require('path');
        return new Promise((resolve, reject) => {
                path.exists(filePath, (exists) => {
                    resolve(exists);
                });
            });
    }

    /**
     * 创建目录
     * @param dirPath {string} 目录路径
     * @returns {Promise<void>}
     */
    async mkdir(dirPath) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.mkdir(dirPath, (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }

    /**
     * 删除目录
     * @param dirPath {string} 目录路径
     * @returns {Promise<void>}
     */
    async rmdir(dirPath) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.rmdir(dirPath, (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }

    /**
     * 读取目录下的文件
     * @param dirPath {string} 目录路径
     * @returns {Promise<string[]>}
     */
    async readdir(dirPath) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.readdir(dirPath, (err, files) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(files);
                }
            });
        });
    }

    /**
     * 重命名文件或目录
     * @param oldPath {string} 旧路径
     * @param newPath {string} 新路径
     * @returns {Promise<void>}
     */
    async rename(oldPath, newPath) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.rename(oldPath, newPath, (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }

    /**
     * 复制文件或目录
     * @param src {string} 源路径
     * @param dest {string} 目标路径
     * @returns {Promise<void>}
     */
    async copy(src, dest) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            const readStream = fs.createReadStream(src);
            const writeStream = fs.createWriteStream(dest);
            readStream.on('error', (err) => {
                reject(err);
            });
            writeStream.on('error', (err) => {
                reject(err);
            });
            writeStream.on('close', () => {
                resolve();
            });
            readStream.pipe(writeStream);
        });
    }

    /**
     * 移动文件或目录
     * @param src {string} 源路径
     * @param dest {string} 目标路径
     * @returns {Promise<void>}
     */
    async move(src, dest) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.rename(src, dest, (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }

    /**
     * 获取文件或目录的状态
     * @param path {string} 路径
     * @returns {Promise<fs.Stats>}
     */
    async stat(path) {
        const fs = require('fs');
        return new Promise((resolve, reject) => {
            fs.stat(path, (err, stats) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(stats);
                }
            });
        });
    }

    /**
     * 选择目录
     */
    selectDir() {
        const that = this;
        const { dialog } = require('electron')
        return dialog.showOpenDialogSync(that.mainWindow, {
            properties: ['openDirectory']
        })
    }

    /**
     * 选择文件
     */
    selectFile() {
        const that = this;
        const { dialog } = require('electron')
        return dialog.showOpenDialogSync(that.mainWindow, {
            properties: ['openFile']
        })
    }

    /**
     * 选择多个文件
     */
    selectFiles() {
        // const that=
    }

    /**
     * 选择保存文件
     */
    async saveFile() {
        const { dialog } = require('electron')
        return new Promise((resolve, reject) => {
            dialog.showSaveDialog({
                properties: ['openFile']
            }, (filePath) => {
                if (filePath) {
                    resolve(filePath);
                } else {
                    reject();
                }
            });
        });
    }
}